package edu.zqu.collegeField.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.FieldDepartment;
import edu.zqu.collegeField.project.mapper.FieldDepartmentMapper;
import edu.zqu.collegeField.project.service.FieldDepartmentService;
import edu.zqu.collegeField.project.vo.FieldDepartmentVo;
import edu.zqu.collegeField.project.vo.FieldVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 场地的管理部门 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class FieldDepartmentServiceImpl extends ServiceImpl<FieldDepartmentMapper, FieldDepartment> implements FieldDepartmentService {
    @Autowired
    private FieldDepartmentMapper fieldDepartmentMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;

    @Override
    public List<FieldDepartmentVo> get(BigInteger collegeId, BigInteger fieldId, BigInteger departmentId) {//获取场地的管理部门列表
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("fieldId", Convert.toStr(fieldId));
        map.put("departmentId", Convert.toStr(departmentId));
        List<FieldDepartmentVo> fieldDepartmentVoList = BeanMapper.mapList(fieldDepartmentMapper.get(map), FieldDepartmentVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (FieldDepartmentVo fieldDepartmentVo : fieldDepartmentVoList) {
            fieldDepartmentVo.setCollegeName(collegeName);
        }
        return fieldDepartmentVoList;
    }

    @Override
    public FieldDepartmentVo getById(BigInteger id) {//获取场地的管理部门
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        FieldDepartmentVo fieldDepartmentVo = BeanMapper.map(fieldDepartmentMapper.getById(map), FieldDepartmentVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(fieldDepartmentVo.getCollegeId());
        fieldDepartmentVo.setCollegeName(collegeName);
        return fieldDepartmentVo;
    }

    @Override
    public int add(FieldDepartment fieldDepartment) {//添加场地的管理部门
        fieldDepartment.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return fieldDepartmentMapper.insert(fieldDepartment);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(FieldDepartment fieldDepartmentUpdate) {//修改场地管理部门
        FieldDepartment fieldDepartment = fieldDepartmentMapper.selectById(fieldDepartmentUpdate.getId());
        if (StringUtils.isNull(fieldDepartment)) {
            throw new NullPointerException("找不到该场地管理部门");
        }
        BeanMapper.copyPropertiesNotNull(fieldDepartmentUpdate, fieldDepartment);
        try {
            int insert = fieldDepartmentMapper.insert(fieldDepartment.setLastVersion(fieldDepartmentUpdate.getId()));
            if (insert >= 1) {
                return delete(fieldDepartmentUpdate.getId(), fieldDepartmentUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(BigInteger id, BigInteger byWho) {//删除场地的管理部门
        try {
            UpdateWrapper<FieldDepartment> fieldDepartmentUpdateWrapper = new UpdateWrapper<FieldDepartment>().eq("id", Convert.toStr(id));
            int update = fieldDepartmentMapper.update(new FieldDepartment().setByWho(byWho), fieldDepartmentUpdateWrapper);
            if (update >= 1) {
                return fieldDepartmentMapper.delete(fieldDepartmentUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
